package com.example.manualexchangemultipledatasourcedemo.service;

import com.example.manualexchangemultipledatasourcedemo.mapper.primaryMapper.PrimaryMapper;
import com.example.manualexchangemultipledatasourcedemo.mapper.secondaryMapper.SecondaryMapper;
import com.example.manualexchangemultipledatasourcedemo.po.AuditLogPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author yj
 * @since 2020/9/14
 */

@Service
public class ConsumerService {

    private final PrimaryMapper primaryMapper;
    private final SecondaryMapper secondaryMapper;

    @Autowired
    public ConsumerService(PrimaryMapper primaryMapper, SecondaryMapper secondaryMapper) {
        this.primaryMapper = primaryMapper;
        this.secondaryMapper = secondaryMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    public void primaryInsert() {
        final AuditLogPo auditLogPo = AuditLogPo.builder().id("1").actor("UI01010").build();
        primaryMapper.insertAuditLog(auditLogPo);
    }

    @Transactional(rollbackFor = Exception.class)
    public void secondaryDelete() {
        secondaryMapper.deleteAuditLogById(628);
    }

    @Transactional(rollbackFor = Exception.class)
    public void AddAndRemoveData() {
        System.out.println("primaryMapper ==>");
        primaryInsert();
        System.out.println(primaryMapper.findAllAuditLog().size());
        System.out.println("secondaryMapper ==>");
        secondaryDelete();
        System.out.println(secondaryMapper.countAll());
    }
}
